package com.fg.cloud.factory.controller.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.system.AgencyCollectionOrderEntity;
import com.fg.cloud.manager.entry.system.CommodityEntity;
import com.fg.cloud.manager.entry.system.StockEntity;
import com.fg.cloud.manager.entry.system.vo.DealerSetVo;
import com.fg.cloud.manager.entry.system.vo.DistributorVo;
import com.fg.cloud.manager.entry.system.vo.OrderVo;
import com.fg.cloud.manager.entry.system.vo.ReceivablesVo;
import com.fg.cloud.manager.service.system.SystemDealerSetService;

/**
  * Description:厂商平台功能参数设置
  * @author 陈银龙
  * @date 2017年8月7日上午11:44:53
 */
@Controller
@RequestMapping("systemDealer")
public class SystemDealerSetController extends BaseController{
	
	@Autowired
	private SystemDealerSetService systemDealerSetService;
	
	/**
	 * Description:根据facId查询系统设置所有参数
	 * @param req
	 * @param resp
	 * @author 陈银龙
	 * @date 2017年8月7日下午1:57:50
	 */
	@RequestMapping(value="findDelByAgeIdAndFacId",method=RequestMethod.POST)
	@ResponseBody
	public void findDelByAgeIdAndFacId(HttpServletRequest req , HttpServletResponse resp){
		try{
			Long facId = getCurrentFactoryUser().getFacId();
			List<DealerSetVo> list = systemDealerSetService.findDelByAgeIdAndFacId(facId);
			if(list.size()>0){// 有数据则查询
				doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_SUCCESS);
			}else{// 无数据则添加
				AgencyCollectionOrderEntity dorvo = new AgencyCollectionOrderEntity();
				dorvo.setFacId(getCurrentFactoryUser().getFacId());
				dorvo.setCreator(getCurrentFactoryUser().getId());
				systemDealerSetService.insertDor(dorvo);
				
				StockEntity sv = new StockEntity();
				sv.setFacId(getCurrentFactoryUser().getFacId());
				sv.setCreator(getCurrentFactoryUser().getId());
				systemDealerSetService.insertStock(sv);
				
				CommodityEntity cv = new CommodityEntity();
				cv.setFacId(getCurrentFactoryUser().getFacId());
				cv.setCreator(getCurrentFactoryUser().getId());
				systemDealerSetService.insertCommodity(cv);
				list = systemDealerSetService.findDelByAgeIdAndFacId(facId);
				doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_SUCCESS);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:查询经销商设置 ,订单设置 ,收款设置表权限
	 * @param req
	 * @param resp
	 * @author 陈银龙
	 * @date 2017年8月7日下午3:31:50
	 */
	public List<AgencyCollectionOrderEntity> findDorByFacId(){
			Long facId = getCurrentFactoryUser().getFacId();
			List<AgencyCollectionOrderEntity> list = systemDealerSetService.findDorByFacId(facId);
			if(list.size()>0){
				return list;
			}else{
				AgencyCollectionOrderEntity dorvo = new AgencyCollectionOrderEntity();
				dorvo.setFacId(getCurrentFactoryUser().getFacId());
				dorvo.setCreator(getCurrentFactoryUser().getId());
				systemDealerSetService.insertDor(dorvo);
				list = systemDealerSetService.findDorByFacId(facId);
				return list;
			}
	}
	
	/**
	 * Description:查询库存设置表权限
	 * @param req
	 * @param resp
	 * @author 陈银龙
	 * @date 2017年8月7日下午3:31:50
	 */
	@RequestMapping(value="inventory/findStockByFacId",method=RequestMethod.POST)
	@ResponseBody
	public void findStockByFacId(HttpServletRequest req , HttpServletResponse resp){
		try{
			Long facId = getCurrentFactoryUser().getFacId();
			List<StockEntity> list = systemDealerSetService.findStockByFacId(facId);
			if(list.size()>0){
				doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_SUCCESS);
			}else{
				StockEntity sv = new StockEntity();
				sv.setFacId(getCurrentFactoryUser().getFacId());
				sv.setCreator(getCurrentFactoryUser().getId());
				systemDealerSetService.insertStock(sv);
				list = systemDealerSetService.findStockByFacId(facId);
				doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_SUCCESS);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:修改库存设置表权限
	 * @param req
	 * @param resp
	 * @param sv
	 * @author 陈银龙
	 * @date 2017年8月8日下午3:54:29
	 */
	@RequestMapping(value="inventory/updateStock",method=RequestMethod.POST)
	@ResponseBody
	public void updateStock(HttpServletRequest req , HttpServletResponse resp, StockEntity sv){
		try{
			if(sv.getId() ==null){
				doResult(resp, false, StatusCode.LACKVALUE, null, "id不能为空");
				return;
			}
			sv.setFacId(getCurrentFactoryUser().getFacId());
			sv.setUpdater(getCurrentFactoryUser().getId());
			int flag = systemDealerSetService.updateStock(sv);
			if(flag > 0){
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.SAVE_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.SAVEERROR, flag, ResultType.SAVE_FAULT);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:查询商品设置表权限
	 * @param req
	 * @param resp
	 * @author 陈银龙
	 * @date 2017年8月7日下午3:31:50
	 */
	@RequestMapping(value="product/findCommodityByFacId",method=RequestMethod.POST)
	@ResponseBody
	public void findCommodityByFacId(HttpServletRequest req , HttpServletResponse resp){
		try{
			Long facId = getCurrentFactoryUser().getFacId();
			List<CommodityEntity> list = systemDealerSetService.findCommodityByFacId(facId);
			if(list.size()>0){
				doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_SUCCESS);
			}else{
				CommodityEntity cv = new CommodityEntity();
				cv.setFacId(getCurrentFactoryUser().getFacId());
				cv.setCreator(getCurrentFactoryUser().getId());
				systemDealerSetService.insertCommodity(cv);
				list = systemDealerSetService.findCommodityByFacId(facId);
				doResult(resp, true, StatusCode.FINDSUCCESS, list, ResultType.FIND_SUCCESS);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:修改商品设置表权限
	 * @param req
	 * @param resp
	 * @param sv
	 * @author 陈银龙
	 * @date 2017年8月9日上午11:03:39
	 */
	@RequestMapping(value="product/updateCommodity",method=RequestMethod.POST)
	@ResponseBody
	public void updateCommodity(HttpServletRequest req , HttpServletResponse resp, CommodityEntity cv){
		try{
			if(cv.getId() ==null){
				doResult(resp, false, StatusCode.LACKVALUE, null, "id不能为空");
				return;
			}
			cv.setFacId(getCurrentFactoryUser().getFacId());
			cv.setUpdater(getCurrentFactoryUser().getId());
			int flag = systemDealerSetService.updateCommodity(cv);
			if(flag > 0){
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.SAVE_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.SAVEERROR, flag, ResultType.SAVE_FAULT);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:查询经销商设置表权限
	 * @param req
	 * @param resp
	 * @author 陈银龙
	 * @date 2017年8月7日下午3:31:50
	 */
	@RequestMapping(value="agency/findDistributor",method=RequestMethod.POST)
	@ResponseBody
	public void findDistributor(HttpServletRequest req , HttpServletResponse resp){
		try{
			List<AgencyCollectionOrderEntity> list = findDorByFacId();
			if(list == null){
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			}else{
				DistributorVo dv = new DistributorVo();
				dv.setAddressFlag(list.get(0).getAddressFlag());
				dv.setFacId(list.get(0).getFacId());
				dv.setId(list.get(0).getId());
				doResult(resp, true, StatusCode.FINDSUCCESS, dv, ResultType.FIND_SUCCESS);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:修改经销商设置表权限
	 * @param req
	 * @param resp
	 * @author 陈银龙
	 * @date 2017年8月7日下午3:31:50
	 */
	@RequestMapping(value="agency/updateDistributor",method=RequestMethod.POST)
	public void updateDistributor(HttpServletRequest req , HttpServletResponse resp, AgencyCollectionOrderEntity dorvo){
		try{
			if(dorvo.getId() ==null){
				doResult(resp, false, StatusCode.LACKVALUE, null, "id不能为空");
				return;
			}
			dorvo.setFacId(getCurrentFactoryUser().getFacId());
			dorvo.setUpdater(getCurrentFactoryUser().getId());
			int flag = systemDealerSetService.updateDor(dorvo);
			if(flag > 0){
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.SAVE_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.SAVEERROR, flag, ResultType.SAVE_FAULT);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:查询订单设置表权限
	 * @param req
	 * @param resp
	 * @author 陈银龙
	 * @date 2017年8月7日下午3:31:50
	 */
	@RequestMapping(value="order/findOrder",method=RequestMethod.POST)
	@ResponseBody
	public void findOrder(HttpServletRequest req , HttpServletResponse resp){
		try{
			List<AgencyCollectionOrderEntity> list = findDorByFacId();
			if(list == null){
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			}else{
				OrderVo ov = new OrderVo();
				ov.setFacId(list.get(0).getFacId());
				ov.setId(list.get(0).getId());
				ov.setDeliveryDateFlag(list.get(0).getDeliveryDateFlag());
				ov.setMinMoney(list.get(0).getMinMoney());
				ov.setMinMoneyFlag(list.get(0).getMinMoneyFlag());
				doResult(resp, true, StatusCode.FINDSUCCESS, ov, ResultType.FIND_SUCCESS);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:修改订单设置 
	 * @param req
	 * @param resp
	 * @param dorvo
	 * @author 陈银龙
	 * @date 2017年8月8日下午3:54:29
	 */
	@RequestMapping(value="order/updateOrder",method=RequestMethod.POST)
	@ResponseBody
	public void updateOrder(HttpServletRequest req , HttpServletResponse resp, AgencyCollectionOrderEntity dorvo){
		try{
			if(dorvo.getId() ==null){
				doResult(resp, false, StatusCode.LACKVALUE, null, "id不能为空");
				return;
			}
			dorvo.setFacId(getCurrentFactoryUser().getFacId());
			dorvo.setUpdater(getCurrentFactoryUser().getId());
			int flag = systemDealerSetService.updateDor(dorvo);
			if(flag > 0){
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.SAVE_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.SAVEERROR, flag, ResultType.SAVE_FAULT);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:查询收款设置表权限
	 * @param req
	 * @param resp
	 * @author 陈银龙
	 * @date 2017年8月7日下午3:31:50
	 */
	@RequestMapping(value="collection/findReceivables",method=RequestMethod.POST)
	@ResponseBody
	public void findReceivables(HttpServletRequest req , HttpServletResponse resp){
		try{
			List<AgencyCollectionOrderEntity> list = findDorByFacId();
			if(list == null){
				doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			}else{
				ReceivablesVo rv = new ReceivablesVo();
				rv.setFacId(list.get(0).getFacId());
				rv.setId(list.get(0).getId());
				rv.setReceivablesFlag(list.get(0).getReceivablesFlag());
				doResult(resp, true, StatusCode.FINDSUCCESS, rv, ResultType.FIND_SUCCESS);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:修改收款设置权限
	 * @param req
	 * @param resp
	 * @param dorvo
	 * @author 陈银龙
	 * @date 2017年8月8日下午3:54:29
	 */
	@RequestMapping(value="collection/updateReceivables",method=RequestMethod.POST)
	@ResponseBody
	public void updateReceivables(HttpServletRequest req , HttpServletResponse resp, AgencyCollectionOrderEntity dorvo){
		try{
			if(dorvo.getId() ==null){
				doResult(resp, false, StatusCode.LACKVALUE, null, "id不能为空");
				return;
			}
			dorvo.setFacId(getCurrentFactoryUser().getFacId());
			dorvo.setUpdater(getCurrentFactoryUser().getId());
			int flag = systemDealerSetService.updateDor(dorvo);
			if(flag > 0){
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.SAVE_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.SAVEERROR, flag, ResultType.SAVE_FAULT);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:修改对帐单设置权限
	 * @param req
	 * @param resp
	 * @param dorvo
	 * @author 陈银龙
	 * @date 2017年8月8日下午3:54:29
	 */
	@RequestMapping(value="collection/updateBill",method=RequestMethod.POST)
	@ResponseBody
	public void updateBill(HttpServletRequest req , HttpServletResponse resp, AgencyCollectionOrderEntity dorvo){
		try{
			if(dorvo.getId() ==null){
				doResult(resp, false, StatusCode.LACKVALUE, null, "id不能为空");
				return;
			}
			dorvo.setFacId(getCurrentFactoryUser().getFacId());
			dorvo.setUpdater(getCurrentFactoryUser().getId());
			int flag = systemDealerSetService.updateDor(dorvo);
			if(flag > 0){
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.SAVE_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.SAVEERROR, flag, ResultType.SAVE_FAULT);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Description:新增经销商设置 ,订单设置 ,收款设置权限(不用,暂留)
	 * @param req
	 * @param resp
	 * @param dorvo
	 * @author 陈银龙
	 * @date 2017年8月8日下午3:54:29
	 */
	//@RequestMapping(value="insertDor",method=RequestMethod.POST)
	@ResponseBody
	public void insertDor(HttpServletRequest req , HttpServletResponse resp, AgencyCollectionOrderEntity dorvo){
		try{
			dorvo.setFacId(getCurrentFactoryUser().getFacId());
			dorvo.setCreator(getCurrentFactoryUser().getId());
			int flag = systemDealerSetService.insertDor(dorvo);
			if(flag > 0){
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.ADD_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.ADD_FAULT);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.ADD_FAULT);
			e.printStackTrace();
		}
	}

	/**
	 * Description:修改经销商设置 ,订单设置 ,收款设置权限(不用)
	 * @param req
	 * @param resp
	 * @param dorvo
	 * @author 陈银龙
	 * @date 2017年8月8日下午3:54:29
	 */	
	//@RequestMapping(value="updateDor",method=RequestMethod.POST)
	@ResponseBody
	public void updateDor(HttpServletRequest req , HttpServletResponse resp, AgencyCollectionOrderEntity dorvo){
		try{
			if(dorvo.getId() ==null){
				doResult(resp, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			dorvo.setFacId(getCurrentFactoryUser().getFacId());
			dorvo.setUpdater(getCurrentFactoryUser().getId());
			int flag = systemDealerSetService.updateDor(dorvo);
			if(flag > 0){
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.SAVE_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.SAVEERROR, flag, ResultType.SAVE_FAULT);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:新增商品设置表权限(不用,暂留)
	 * @param req
	 * @param resp
	 * @param cv
	 * @author 陈银龙
	 * @date 2017年8月9日上午11:01:38
	 */
	//@RequestMapping(value="insertCommodity",method=RequestMethod.POST)
	@ResponseBody
	public void insertCommodity(HttpServletRequest req , HttpServletResponse resp, CommodityEntity cv){
		try{
			cv.setFacId(getCurrentFactoryUser().getFacId());
			cv.setCreator(getCurrentFactoryUser().getId());
			int flag = systemDealerSetService.insertCommodity(cv);
			if(flag > 0){
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.ADD_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.ADD_FAULT);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.ADD_FAULT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Description:新增库存设置表权限(不用,暂留)
	 * @param req
	 * @param resp
	 * @param sv
	 * @author 陈银龙
	 * @date 2017年8月7日下午3:31:50
	 */
	//@RequestMapping(value="insertStock",method=RequestMethod.POST)
	@ResponseBody
	public void insertStock(HttpServletRequest req , HttpServletResponse resp, StockEntity sv){
		try{
			sv.setFacId(getCurrentFactoryUser().getFacId());
			sv.setCreator(getCurrentFactoryUser().getId());
			int flag = systemDealerSetService.insertStock(sv);
			if(flag > 0){
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.ADD_SUCCESS);
			}else{
				doResult(resp, true, StatusCode.SAVESUCCESS, flag, ResultType.ADD_FAULT);
			}
		}catch (Exception e) {
			doResult(resp, false, StatusCode.SAVEERROR, null, ResultType.ADD_FAULT);
			e.printStackTrace();
		}
	}
}
